package me.ohaga
{
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.World;
	import net.flashpunk.graphics.Image;
	
	public class Game extends World
	{
		[Embed(source="/assets/gfx/door.png")] private static const DOOR_IMG:Class;
		[Embed(source = "assets/gfx/tiles.png")] private var TILES_IMG:Class;
		
		[Embed(source = "assets/maps/testmap.tmx", mimeType="application/octet-stream")] private var LEVEL_1:Class;
		[Embed(source = "assets/maps/testmap2.tmx", mimeType="application/octet-stream")] private var LEVEL_2:Class;

		
		private var _player:Player;
		private var _gravity:Number = 500;
		private var _friction:Number = 100;
		private var _currentLevel:int = 0;
		
		private const MAX_LEVELS:int = 2;
		
		public function Game() {
			super();
		}
		
		override public function begin():void {
			super.begin();
			//FP.world = new LevelResource();
			setupLevel();
		}
		
		public function changeLevel():void {
			clearLevel();
			if(_currentLevel >= MAX_LEVELS)
			{
				FP.world = new Credits();
			}
			else
			{
				setupLevel();
			}
		}
		
		public function clearLevel():void {
			removeAll();
			_player = null;
		}
		
		public function setupLevel():void {
			// TODO: make this cycle through levels by reading xml files. for now, we setup everything the same everytime. I'm Interesing!
			
			// TEST - floors
			/*
			for (var i:Number = 0; i < FP.screen.width/Block.WIDTH; i++) {
				add(new Block(i*Block.WIDTH, 400));
				add(new Block(i*Block.WIDTH, 0)) 
			}
			
			for (i = 0; i < FP.screen.height/Block.HEIGHT; i++) {
				add(new Block(700, i*Block.HEIGHT));
				add(new Block(0, i*Block.HEIGHT));
			}
			*/
			// TEST - door
			
			// Player start
			/*
			_player = new Player(100,360);
			add(_player);
			*/
			_currentLevel++;
				
			var rawData:ByteArray = new this["LEVEL_" + _currentLevel.toString()] as ByteArray;
			var dataString:String = rawData.readUTFBytes(rawData.length);
			var xmlData:XML = new XML(dataString);
			
			//trace(xmlData);
			var cols:uint = xmlData.@width;
			var rows:uint = xmlData.@height;
			var tileW:uint = xmlData.@tilewidth;
			var tileH:uint = xmlData.@tileheight;
			
			var tiles:XMLList = xmlData.layer.(@name == "mapLayer").data.tile;
			var types:XMLList = xmlData.tileset.tile;
			trace(tiles);
			
			var typeArray:Array = new Array();
			
			for each (var tile : XML in types)
			{
				trace(tile.attributes());
				trace(tile.properties.property.@value);
				typeArray[tile.attributes()] = tile.properties.property.@value;
			}
			trace(typeArray);
			var tx:uint = 0;
			var ty:uint = 0;
			for each (var t : XML in tiles)
			{
				//trace(t.@gid);
				var x:int = t.@gid - 1;
				if(typeArray[x] == "wall")
				{
					add(new Block(tx*tileW, ty*tileH));
				}
				else if (typeArray[x] == "start")
				{
					_player = new Player(tx*tileW, (ty-1)*tileH);
					add(_player);
				}
				else if (typeArray[x] == "exit")
				{
					trace("exit: "+tx+", "+ty);
					//exit shit
					var door:Entity = new Entity(tx*tileW,ty*tileH, new Image(DOOR_IMG));
					door.setHitbox(20,20);
					door.type = "exit";
					add(door);
				}
				if(tx+1 < cols)
				{
					tx++;
				}
				else
				{
					ty++;
					tx=0;
				}
			}
		}
		
		public function get gravity():Number { return _gravity; }
		public function get friction():Number { return _friction; }
	}
}